// pages/cart/coupon/coupon.js
const Controllers = require('../../controllers/Controller.js').controller;
const formatTime_2 = require("../../../utils/util").formatTime_2
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    isSelect: false,
    shopId: '',
    couponList: [],
    totalPrice:0,
    goodsIds:'',
    couponAccountId:''
  },
  //选择暂不使用
  select() {
    let isSelect = this.data.isSelect
    isSelect = !isSelect
    let couponList = this.data.couponList;
    couponList.forEach(item => {
      if (isSelect) {
        item.select = false
      }
    })
    this.setData({
      isSelect,
      couponList
    })
  },
  //确定按钮
  confirm() {
    let selectCouponList
    selectCouponList = this.data.couponList.filter(item => item.select)
    if (selectCouponList.length > 0) {
      app.globalData.coupon = selectCouponList[0]
      app.globalData.isuseCoupon=1
    } else {
      app.globalData.isuseCoupon=2
    }
    wx.navigateBack({
      delta: 1
    })
  },
  //跳转详情
  gopage(e){
    let url = e.currentTarget.dataset['url'];
    wx.navigateTo({
      url: url
    })
  },
  //选择优惠券
  selectCoupon(e) {
    let index = e.currentTarget.dataset['index'];
    let couponList = this.data.couponList;
    couponList.forEach(item => {
      item.select = false
    })
    couponList[index].select = !couponList[index].select;
    this.setData({
      couponList,
      isSelect: false
    })
  },
  myInformation() {
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
    //获取数据
    Controllers.myInformation({ shopId: this.data.shopId }).then(res => {
      this.setData({
        userId: res.data.id
      })
      this.getMyCouponPage()
    })
  },
  //获取未使用的优惠券列表
  getMyCouponPage() {
    Controllers.canUseCoupon({ shopId: this.data.shopId, accountId: this.data.userId,goodsIds:this.data.goodsIds,totalPrice:this.data.totalPrice }).then(res => {
      if (res.code == 1) {
        res.data.forEach(element => {
          element.select = false
          if(this.data.couponAccountId==element.id){
            element.select = true
          }
          if(element.timeState == 1){
           if(element.effectiveStartTime){
            element.effectiveStartTime = formatTime_2(element.effectiveStartTime.replace(/-/g, '/'))
            element.effectiveEndTime = formatTime_2(element.effectiveEndTime.replace(/-/g, '/'))

            element.effectiveStartTime= element.effectiveStartTime.split(" ")[0]
            element.effectiveEndTime= element.effectiveEndTime.split(" ")[0]
           }
          }
        });
        // res.data = res.data.concat(res.data.other)
        this.setData({
          couponList: res.data
        })
        console.log(this.data.couponList)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({globalStype:app.globalData.globalStype})
    if (app.globalData.shopId) {
      this.setData({
        shopId: app.globalData.shopId
      })
    }
    if(options.goodsIds){
      this.setData({
        goodsIds:options.goodsIds
      })
    }
    if(options.totalPrice){
      this.setData({
        totalPrice:options.totalPrice
      })
    }
    if(options.couponAccountId){
      this.setData({
        couponAccountId:options.couponAccountId
      })
    }
    this.myInformation()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})